package com.atguigu.ssyx.activity.controller;


import com.atguigu.ssyx.activity.service.ActivityInfoService;
import com.atguigu.ssyx.common.result.Result;
import com.atguigu.ssyx.model.activity.ActivityInfo;
import com.atguigu.ssyx.model.product.SkuInfo;
import com.atguigu.ssyx.vo.activity.ActivityRuleVo;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 活动表 前端控制器
 * </p>
 *
 * @author atguigu
 * @since 2025-09-17
 */
@Api(tags = "活动信息管理接口")
@RestController
@RequestMapping("/admin/activity/activityInfo")
public class ActivityInfoController {
    @Autowired
    private ActivityInfoService activityInfoService;

    @ApiOperation("获取活动列表")
    @GetMapping("/{page}/{limit}")
    public Result getActivityInfoList(@PathVariable("page") Long page, @PathVariable("limit") Long limit) {
        Page<ActivityInfo> pageparam = new Page<>(page, limit);
        IPage<ActivityInfo> pageModel = activityInfoService.selectPage(pageparam);
        return Result.ok(pageModel);
    }
    @ApiOperation("添加活动")
    @PostMapping("/save")
    public Result saveActivityInfo(@RequestBody ActivityInfo activityInfo) {
        activityInfoService.save(activityInfo);
        return Result.ok(null);
    }
    @ApiOperation("修改活动")
    @PutMapping("/update")
    public Result updateActivityInfo(@RequestBody ActivityInfo activityInfo) {
        activityInfoService.updateById(activityInfo);
        return Result.ok(null);
    }
    @ApiOperation("删除单个活动")
    @DeleteMapping("/remove/{id}")
    public Result removeActivityInfo(@PathVariable("id") Long id) {
        activityInfoService.removeById(id);
        return Result.ok(null);
    }

    @ApiOperation("批量删除活动")
    @DeleteMapping("/batchRemove")
    public Result batchRemove(@RequestBody List<Long> idList) {
        activityInfoService.removeByIds(idList);
        return Result.ok(null);
    }

    //营销活动规则相关接口
    //1.根据活动id获取活动规则数据
    @ApiOperation("根据活动id获取活动规则数据")
    @GetMapping("/findActivityRuleList/{id}")
    public Result findActivityRuleList(@PathVariable("id") Long id) {
       Map<String, Object> activityRuleMap=
               activityInfoService.findActivityRuleList(id);
        return Result.ok(activityRuleMap);
    }
    //2.在活动里面添加规则的数据
    @ApiOperation("在活动里面添加规则的数据")
    @PostMapping("/saveActivityRule")
    public Result saveActivityRule(@RequestBody ActivityRuleVo activityRuleVo) {
        activityInfoService.saveActivityRule(activityRuleVo);
        return Result.ok(null);
    }

    //3.根据关键字来查询他匹配的sku信息
    @ApiOperation("根据关键字来查询他匹配的sku信息")
    @GetMapping("/findSkuInfoByKeyword/{keyword}")
    public Result findSkuInfoByKeyword(@PathVariable("keyword") String keyword) {
       List<SkuInfo> list=activityInfoService.findSkuInfoByKeyword(keyword);
        return Result.ok(list);
    }

    @ApiOperation("通过id查询规则")
    @GetMapping("/get/{id}")
    public Result getActivityInfo(@PathVariable("id") Long id) {
        ActivityInfo activityInfo = activityInfoService.getById(id);
        activityInfo.setActivityTypeString(activityInfo.getActivityType().getComment());
        return Result.ok(activityInfo);
    }

}

